<template>
  <div class="app-container">
    <el-row :gutter="10" class="search-top">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="凭证名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入凭证名称"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-button slot="append" icon="el-icon-search" @click="handleQuery" ></el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="1.5" style="float: right;">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['credential:credential:add']"
        >新增凭证</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['credential:credential:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="credentialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80"/>
      <el-table-column label="授权类型" align="center" prop="accessTarget">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kw_credential_access_type" :value="scope.row.accessTarget"/>
        </template>
      </el-table-column>
      <el-table-column label="凭证名称" align="center" prop="name" />
      <el-table-column label="用户名" align="center" prop="username"/>
      <el-table-column label="密码" align="center" prop="secret" />
      <el-table-column label="备注" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['credential:credential:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['credential:credential:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户凭证信息对话框 -->
    <add-credential v-if="open" :id="id" @closeDrawer="getList"/>
  </div>
</template>

<script>
import { listCredential, delCredential} from "@/api/credential/credential";
import AddCredential from "@/views/system/user/credential/addCredential";
export default {
  name: "Credential",
  dicts: ['kw_credential_access_type'],
  components:{AddCredential},
  data() {
    return {
      // 遮罩层
      loading: true,
      id: undefined,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户凭证信息表格数据
      credentialList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        accessTarget: null,
        name: null,
        secret: null,
        description: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户凭证信息列表 */
    getList() {
      this.open = false;
      this.loading = true;
      listCredential(this.queryParams).then(response => {
        this.credentialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.id=undefined;
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id=row.id;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户凭证信息编号为"' + ids + '"的数据项？').then(function() {
        return delCredential(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('credential/credential/export', {
        ...this.queryParams
      }, `credential_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
